{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a3123db5",
   "metadata": {},
   "source": [
    "# 1.语音录制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1da36395",
   "metadata": {},
   "source": [
    "> 1.SpeechRecognition       \n",
    "> 2.Pyaudio       \n",
    "> 3.pydub       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a80ab0c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "562e800c",
   "metadata": {},
   "source": [
    "# 2.语音识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2e5a61ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备自己的key\n",
    "API_KEY = 'AxMWfmsmYtSdgxrlG8tkZfvT'\n",
    "SECRET_KEY = 'srinuXLjBUCqYywaD69eSntdDeQjBUpy'\n",
    "\n",
    "#文件路径\n",
    "AUDIO_FILE = 'speech-demo-master/rest-api-asr/python/audio/16k.m4a'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b878e65",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46c1e184",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86bb5d59",
   "metadata": {},
   "outputs": [],
   "source": [
    "xiao_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0feb986",
   "metadata": {},
   "source": [
    "# 3.语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6476b8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 智超 ：百度密钥\n",
    "API_KEY = 'smxkOHWjqLVljEmIry5vuSYI'\n",
    "SECRET_KEY = 'ky3FZeSSDurxLyLLqZ2kPaEnts9NH1W1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be2cb17e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7bb6c016",
   "metadata": {},
   "outputs": [],
   "source": [
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "xu_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9ef653c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9416818f",
   "metadata": {},
   "outputs": [],
   "source": [
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "asr_output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "347e06e0",
   "metadata": {},
   "source": [
    "# 连接OpenAi"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e16b37e",
   "metadata": {},
   "source": [
    "* 示例代码"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a650ed0e",
   "metadata": {},
   "source": [
    "import openai #主循环\n",
    "\n",
    "def openai_chat(question):"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40602f45",
   "metadata": {},
   "source": [
    "api_key = \"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\"\n",
    "\n",
    "openai.api_key = api_key\n",
    "\n",
    "#获取用户输入的问题\n",
    "#question = input(\"请输入问题：\")\n",
    "\n",
    "#调用openai的api\n",
    "completion = openai.ChatCompletion.create(\n",
    "    model=\"gpt-3.5-turbo\",\n",
    "    messages=[{\"role\": \"user\",\n",
    "                 \"content\": question}],\n",
    "    max_tokens=4000,\n",
    "    temperature=0.8,\n",
    ")\n",
    "#打印答案\n",
    "return completion[\"choices\"][0][\"message\"][\"content\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d7fefdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d10d6c22",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat = opchat.openai_chat(asr_output)\n",
    "chat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a79efb25",
   "metadata": {},
   "source": [
    "# 在jupyter中播放音频文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "302c1274",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('1.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6687eb6f",
   "metadata": {},
   "source": [
    "# 智能对话机器人最终版"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9747c543",
   "metadata": {},
   "source": [
    "# 语音合成测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02c67605",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition # 语音音频录制模块\n",
    "from pydub import AudioSegment # 播放音频模块\n",
    "from pydub.playback import play # 播放音频模块\n",
    "from bdasr import fetch_token,asr # token 和语音识别模块\n",
    "from bdtts import tts # 语音合成模块\n",
    "\n",
    "# 1. 录制音频\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到 wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "print(\"----完成录制----\")\n",
    "    \n",
    "# 2. 语音识别\n",
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "print(\"----完成语音识别----\",asr_output)\n",
    "\n",
    "# 3. openai- API\n",
    "chat = opchat.openai_chat(asr_output)\n",
    "print(chat)\n",
    "\n",
    "# 4. 语音合成 TTS -->产生result.wav\n",
    "tts(xu_token,chat)\n",
    "print(\"----完成合成---\")\n",
    "\n",
    "# 5. 在jupyter中进行音频播放\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)\n",
    "print(\"---完成播放---\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
